package com.mall.controller;

import com.mall.domain.Member;
import com.mall.domain.Page;

import com.mall.service.impl.MemberServiceImpl;
import com.mall.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : liangY
 * @date : 2020-12-30 12:39
 */

@Controller
@RequestMapping("/members")
public class MemberController {

    @Autowired
    private MemberServiceImpl memberService;
    /**
     * 查询所有会员
     * @param page
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(@RequestBody Page page){
        Map<String,Object> memberMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //获取会员集合
        List<Member> members = memberService.findAll(page);
        //放入dataMap集合
        dataMap.put("members",members);
        dataMap.put("total",memberService.findTotal());
        //放入Map集合
        memberMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取会员列表成功");
        metaMap.put("status",200);
        memberMap.put("meta",metaMap);
        return memberMap;
    }


    /**
     * 用户登录
     * @param loginUser
     * @return
     */
    @RequestMapping("/login")
    public  @ResponseBody
    Map<String,Object> login(@RequestBody Member loginUser) {
        Map<String,Object> loginMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        String token = TokenUtil.sign(loginUser.getUsername());
        Member member = memberService.login(loginUser);
        dataMap.put("token",token);
        dataMap.put("user",member);
        loginMap.put("data",dataMap);
        Map<String,Object> metaMap = new HashMap<>();
        if(member != null){
            metaMap.put("msg","登录成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","登录失败");
            metaMap.put("status",0);
        }
        loginMap.put("meta",metaMap);
        return  loginMap;

    }

    /**
     * 更新指定会员
     * @param member
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody
    Map<String, Object> update(@RequestBody Member member){
        Map<String,Object> memberMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("member",member);
        //放入Map集合
        memberMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = memberService.update(member);
        if(flag > 0){
            metaMap.put("msg","更新会员成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新会员失败");
            metaMap.put("status",0);
        }
        memberMap.put("meta",metaMap);
        return memberMap;
    }
    @RequestMapping("/update2")
    public @ResponseBody
    Map<String, Object> update2(@RequestBody Member member){
        Map<String,Object> memberMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("member",member);
        //放入Map集合
        memberMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = memberService.update2(member);
        if(flag > 0){
            metaMap.put("msg","更新会员成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新会员失败");
            metaMap.put("status",0);
        }
        memberMap.put("meta",metaMap);
        return memberMap;
    }
    /**
     * 添加会员
     * @param member
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody
    Map<String, Object> add(@RequestBody Member member){
        Map<String,Object> memberMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();

        //放入dataMap集合
        dataMap.put("member",member);
        //放入Map集合
        memberMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = memberService.add(member);
        if(flag > 0){
            metaMap.put("msg","添加会员成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新会员失败");
            metaMap.put("status",0);
        }
        memberMap.put("meta",metaMap);
        return memberMap;
    }

    /**
     * 删除会员
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public @ResponseBody
    Map<String, Object> add(Integer id){
        Map<String,Object> memberMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        memberMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = memberService.del(id);
        if(flag > 0){
            metaMap.put("msg","删除会员成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除会员失败");
            metaMap.put("status",0);
        }
        memberMap.put("meta",metaMap);
        return memberMap;
    }
}
